package com.cea.papyrus.core.actions.imports;

import java.util.ArrayList;
import java.util.Iterator;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.edit.domain.EditingDomain;
import org.eclipse.gef.commands.Command;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.PlatformUI;
import org.eclipse.uml2.common.edit.command.ChangeCommand;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.Package;
import org.eclipse.uml2.uml.VisibilityKind;

import com.cea.papyrus.core.PapyrusPlugin;
import com.cea.papyrus.core.commands.EMFtoGEFWrapCommand;
import com.cea.papyrus.core.ui.dialogs.PackageImportTreeSelectionDialog;

/**
 * Action that imports package from workspace models
 */
public class ImportPackageFromFileAction extends ImportPackageAction {

	/** request key for the request associated to this action  */
	public final static String IMPORT_PACKAGE_FROM_FILE_ACTION_REQUEST = ImportPackageFromFileAction.class.getName()+ "Request";

	/** ID of  this action */
	public final static String IMPORT_PACKAGE_FROM_FILE_ACTION_ID = ImportPackageFromFileAction.class.getName()+ "ID";
	
	/**
	 * Creates a new ImportLibraryFromRepositoryAction
	 * @param part that part which contains the selection
	 */
	public ImportPackageFromFileAction(IWorkbenchPart part) {
		super(part);
		setText("Import Package from file");
		setId(IMPORT_PACKAGE_FROM_FILE_ACTION_ID);
		setToolTipText("Import Package from models located on the file system");

		// icon for action
		ImageDescriptor descriptor = PapyrusPlugin.getImageDescriptor(PapyrusPlugin.PAPYRUS_ICONS_16x16+"ImportPackageFile.gif");
		setImageDescriptor(descriptor);
		setHoverImageDescriptor(descriptor);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected boolean calculateEnabled() {
		// look the selection.
		// if one of the element is a package and is not an imported package, it is enabled
		return(getSelectedPackage()!=null);
	}
	
	/**
	 * {@inheritDoc}
	 */
	public Command createCommand(EditingDomain editingDomain) {
		return new ImportPackageCommand(editingDomain);
	}

	
	/**
	 * EMF change Command wrapped in a GEF command to import packages from Workspace.
	 */
	public class ImportPackageCommand extends EMFtoGEFWrapCommand {
		
		/**
		 * Creates a new ImportPackageCommand.
		 * @param editingDomain editing doamin used by the EMF command
		 */
		public ImportPackageCommand(EditingDomain editingDomain) {
			super(editingDomain);
		}
		
		/**
		 * {@inheritDoc}
		 */
		public org.eclipse.emf.common.command.Command createEMFCommand() {
			Runnable runnable = new Runnable() {
				public void run() {
					final Package selectedPackage = getSelectedPackage();
					if(selectedPackage!=null) {
						FileDialog fd =
							new FileDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow()
									.getShell());
						String[] filterExt = {
								"*.uml"
						};
						fd.setFilterExtensions(filterExt);
						String selectedFile = fd.open();

						if (selectedFile != null) {
							URI modelUri = URI.createFileURI(selectedFile);
							Resource modelResource = resourceSet.getResource(modelUri, true);
							PackageImportTreeSelectionDialog eisd =
								new PackageImportTreeSelectionDialog(PlatformUI.getWorkbench()
										.getActiveWorkbenchWindow().getShell(),
										((Package) modelResource.getContents().get(0)));
							int ret = eisd.open();

							if (ret == Window.OK) {
								ArrayList result = eisd.getResult();
								Iterator resultIter = result.iterator();
								while (resultIter.hasNext()) {
									Element element = (Element) resultIter.next();
									selectedPackage.createPackageImport((Package)element, VisibilityKind.PUBLIC_LITERAL);
								}
							}
						}
					}
				}
			};
			return new ChangeCommand(editingDomain, runnable, "Import Packages");
		}
	}
	
}